<?php
/**
 * restaurant store information
 */
class store_model extends CI_Model {
    private $error;
    private $is_valid_schema;
	/**
	 * use constructor to pre load database object
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

    /**
     * get store detail information
     * 
     * @param bool $encode_html
     * 
     * @return array store_info
     */
	public function get_store_info($encode_html = true)
	{
		$this->db->limit(1);
		$query = $this->db->get('store_info');
	    $result = $query->row_array();
        if($encode_html){
            foreach($result as $key => $value){
                if(is_string($value)){
                    $result[$key] = htmlentities($value, ENT_QUOTES, "UTF-8");
                }
            }
        }
        return $result;
	}
    /**
     * update store info, return error array (empty if everything is fine)
     * 
     * @param string $phone
     * @param string $name
     * @param string $address
     * @param string $city
     * @param string $state
     * @param string $zipcode
     * @param string $footer
     * @param string $taxrate
     * 
     * @return array errors
     */
	public function update_store_info($phone, $name, $address, $city, $state, $zipcode, $footer, $taxrate)
	{
        $this->error = array('user' => array(), 'system' => array());
        
        /**
         * data after trim is real data need to be validate and update on database
         */
        $name =trim($name);
        $phone = trim($phone);
        $address = trim($address);
        $city = trim($city);
        $state = trim($state);
        $zipcode = trim($zipcode);
        $footer = trim($footer);
        $taxrate = trim($taxrate);

        $result = $this->_schema_validation($phone, $name, $address, $city, $state, $zipcode, $footer, $taxrate);
        if(true === $this->is_valid_schema){
            $data = array(
                "phone" => $phone,
                "name"  => $name,
                "address" => $address,
                "city" => $city,
                "state" => $state,
                "zipcode" => $zipcode,
                "footer" => $footer,
                "taxRate" => $taxrate
            );
            $this->db->update('store_info', $data);
        }
        return $this->error;
	}
    /**
     * validate against store_info table schema
     * 
     * @param string $phone
     * @param string $name
     * @param string $address
     * @param string $city
     * @param string $state
     * @param string $zipcode
     * @param string $footer
     * @param string $taxrate
     *
     * @return void
     */
    private function _schema_validation($phone, $name, $address, $city, $state, $zipcode, $footer, $taxrate){

        $this->is_valid_schema = false;
        $user_error_intro_count = count($this->error['user']);
        $this->load->library('validation/validation_string');

        // phone should interger and 10 digits exact
        $this->validation_string->set_data($phone)->is_natural()->exact_length(10);
        if(FALSE === $this->validation_string->result()){
            $this->error['user']['phone'] = "phone should 10 digits exact";
        }

        // name less than 64 chars
        $this->validation_string->set_data($name)->max_length(64)->min_length(1);
        if(FALSE === $this->validation_string->result()){
            $this->error['user']['name'] = "store name need be 1 - 64 characters";
        }

        // address less than 100 chars
        $this->validation_string->set_data($address)->max_length(100)->min_length(1);;
        if(FALSE === $this->validation_string->result()){
            $this->error['user']['address'] = "address need be 1 - 100 characters";
        }

        // city less than 64 chars
        $this->validation_string->set_data($city)->max_length(64)->min_length(1);;
        if(FALSE === $this->validation_string->result()){
            $this->error['user']['city'] = "city name need be 1 - 64 characters";
        }

        // state less than 32 chars
        $this->validation_string->set_data($state)->max_length(32)->min_length(1);;
        if(FALSE === $this->validation_string->result()){
            $this->error['user']['state'] = "state name need be 1 - 32 characters";
        }

        // zipcode less than 10 chars, and in format of ddddd-dddd, US format
        $this->validation_string->set_data($zipcode)->is_us_zipcode()->min_length(1);;
        if(FALSE === $this->validation_string->result()){
            $this->error['user']['zipcode'] = "zipcode should be in format of ddddd-dddd, d = digit";
		}

        // footer less than 200 chars
        $this->validation_string->set_data($footer)->max_length(200);
        if(FALSE === $this->validation_string->result()){
            $this->error['user']['footer'] = "footer need be less than 200 characters";
		}

        // taxrate should between 0 - 100
        $tax = floatval($taxrate);
        if(!is_numeric($taxrate) || $tax<0 || $tax>100){
            $this->error['user']['tax'] = "tax rate should only contains number, dot and be positive between 0 and 100";
        }

        $user_error_outro_count = count($this->error['user']);

        if($user_error_intro_count === $user_error_outro_count){
            $this->is_valid_schema = true;
        }
    }
}

/* End of file store_model.php */
/* Location: ./system/application/models/store_model.php */
